<template>
  <div class="returnPassword">
    <header>找回密码</header>
    <div class="mainForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" status-icon>
        <el-form-item label="手机号" prop="tel">
          <el-input type="text" v-model="form.tel" auto-complete="off" placeholder="请输入手机号码"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code" style="height: 3em;">
          <el-input type="text" v-model="form.code" auto-complete="off" placeholder="请输入验证码"></el-input>
          <el-button type="primary" style="position: relative;left: 15em;top: -3em;" @click="getCode" :disabled="btnStatus">获取验证码</el-button>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="form.pass" autocomplete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="form.checkPass" autocomplete="off" placeholder="确认密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="Reset">找回密码</el-button>
          <el-button @click="clearInput" >重置</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>

export default {
  name: "FindPassword",
  data(){
    var checkphone = (rule, value, callback) => {
      // let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!this.isCellPhone(value)) { // 引入methods中封装的检查手机格式的方法
        callback(new Error('请输入正确的手机号!'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.form.validateField('checkPass');
        }
        callback();
      }
    };
    var checkCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入4位的验证码'))
      } else {
        const reg = /^[0-9]*$/ // 验证没有空格的英文名
        // const reg = /^[a-zA-Z][A-Za-z\s]*[a-zA-Z]*$/ 验证有空格的英文名
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的验证码'))
        }
      }

    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      btnStatus:false,
      resetQuery:{
        tel: '',
        pwd: ''
      },
      form: {
        account:'',
        tel:'',
        code:'',
        pass:'',
        checkPass:''

      },
      rules:{
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        tel:[
          {validator: checkphone, trigger:'blur'},
        ],
        code:[
          {validator: checkCode, trigger:'blur'},
        ]
      },
      // 检查手机号
      isCellPhone (val) {
        if (!/^1(3|4|5|6|7|8)\d{9}$/.test(val)) {
          return false
        } else {
          return true
        }
      }
    }
  },
  methods:{
    clearInput(){
      this.form.tel = ''
      this.form.account = ''
      this.form.code = ''
      this.form.pass = ''
      this.form.checkPass = ''
    },
    onSubmit() {
      console.log('submit!');
    },
    Reset(){
      this.$refs.form.validate(valid=>{
        if(valid){
          this.getRequest(`/cms/user/getYzm/${this.form.tel}/${this.form.code}`).then((resp)=>{
            if(resp.status==200){
              this.resetQuery.tel = this.form.tel
              this.resetQuery.pwd = this.form.pass
              this.postRequest('/cms/user/resetPasswd',this.resetQuery).then((resp)=>{
                if(resp.status===200){
                  this.$message.success(resp.message)
                  this.$router.push('/login')
                }else{
                  this.$message.error(resp.message)
                }
              })
            }else{
              this.$message.error('手机号或验证码输入不正确。')
            }
          })
        }
      })
    },
    btnStatusChange(){
      this.btnStatus = false
    },
    getCode(){
      this.$refs.form.validateField('tel',(telError)=>{
        if(telError){
          this.$message.error('请输入手机号码！')
        }else{
          this.getRequest('/cms/user/getYzm/'+this.form.tel).then((resp)=>{
            console.log(resp)
            if(resp.status == 200){
              this.btnStatus = true
              setInterval(this.btnStatusChange,60000)
              this.$message.success('验证码发送成功。')
            }else{
              this.$message.error('验证码发送失败。')
            }
          })
        }
      })

    }
  }
}
</script>

<style scoped>
  .returnPassword{
    width: 940px;
    height: 550px;
    border:solid 1px lightgray;
    margin: 1em auto;
  }
  .returnPassword header{
    font-size: 22px;
    color: #333;
    line-height: 30px;
    padding: 50px 0;
    text-align: center;
  }
  .mainForm{
    width: 400px;
    height: 456px;
    /*border:solid 1px red;*/
    margin: 0 auto;
  }
</style>